package Java.BlackHorse.Stream;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class StreamDemo9 {
    /*
     * collect(Collector collector)  收集流中的数据，放到集合中(List Set Map)
     *
     * 注意：Map集合中键是不能重复的
     * */
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张三-男-18","李四-女-19","王五-男-20","沈六-男-22");

        //收集到List集合中
        List<String> newList=list.stream().filter(s -> "男".equals(s.split("-")[1])).collect(Collectors.toList());
        System.out.println(newList);

        //收集到Set集合中
       Set<String> newSet = list.stream().filter(s ->"男".equals( s.split("-")[1])).collect(Collectors.toSet());
        System.out.println(newSet);

        //收集到Map集合中
        Map<String,Integer> newMap = list.stream().collect(Collectors.toMap(new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s.split("-")[0];
            }
        }, new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s.split("-")[2]);
            }
        }));
        Set<Map.Entry<String,Integer>> entries = newMap.entrySet();
        for(Map.Entry<String,Integer> entry : entries ){
            System.out.print(entry.getKey()+":"+entry.getValue());
            System.out.println();
        }
    }
}
